package ZuoYe.I;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 改密界面
 */
public class ChangePwdPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon sss = new ImageIcon("picture/buttonSure.jpg");
    private JLabel pwdLabel1 = new JLabel("原密码：");
    private JLabel pwdLabel2 = new JLabel("新密码：");
    private JLabel pwdLabel3 = new JLabel("确认新密码：");
    private JPasswordField pwdField1 = new JPasswordField();
    private JPasswordField pwdField2 = new JPasswordField();
    private JPasswordField pwdField3 = new JPasswordField();
    private Font font = new Font("黑体",Font.BOLD,20);
    private JButton loginBtn = new JButton(sss);
    public ChangePwdPanel(ATMFrame atmFrame){
        this.atmFrame = atmFrame;
        this.setLayout(null);
        pwdLabel1.setBounds(300,200,120,50);
        pwdLabel1.setFont(font);
        pwdLabel2.setBounds(300,300,120,50);
        pwdLabel2.setFont(font);
        pwdLabel3.setBounds(300,400,120,50);
        pwdLabel3.setFont(font);
        pwdField1.setBounds(500,200,180,50);
        pwdField1.setFont(font);
        pwdField2.setBounds(500,300,180,50);
        pwdField2.setFont(font);
        pwdField3.setBounds(500,400,180,50);
        pwdField3.setFont(font);
        loginBtn.setBounds(400,500,140,50);
        loginBtn.addActionListener(this);
        this.add(pwdLabel1);
        this.add(pwdLabel2);
        this.add(pwdLabel3);
        this.add(pwdField1);
        this.add(pwdField2);
        this.add(pwdField3);
        this.add(loginBtn);
        this.setOpaque(false);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==loginBtn){
            User user = atmFrame.getLoginUser();
            if (!new String(pwdField1.getPassword()).equals(user.getPwd())){
                JOptionPane.showMessageDialog(this,"原密码输入错误");
                return;
            }
            if (new String(pwdField2.getPassword()).equals("")){
                JOptionPane.showMessageDialog(this,"请输入新密码");
                return;
            }
            if (new String(pwdField3.getPassword()).equals("")){
                JOptionPane.showMessageDialog(this,"请再次输入新密码");
                return;
            }
            if (new String(pwdField2.getPassword()).equals(user.getPwd())){
                JOptionPane.showMessageDialog(this,"不能和原密码一样");
                return;
            }
            if (!new String(pwdField3.getPassword()).equals(new String(pwdField2.getPassword()))){
                JOptionPane.showMessageDialog(this,"两次密码不一致");
                return;
            }
            user.setPwd(new String(pwdField3.getPassword())+"");
                JOptionPane.showMessageDialog(this,"密码修改成功");
            atmFrame.switchPanel("login");
        }
    }
}
